package com.kevin.thread.ticketWindow;

import java.util.concurrent.TimeUnit;

/**
 * @Description     模拟营业厅叫号程序-实现Runnable
 * @Author Kevin
 * @Date: Created in  15:47 2019/9/10:
 * @Modified By:
 * @Email: tang.lx@topcheer.com
 */
public class TicketWindowRunnable implements Runnable{

    // 最多受理50笔业务
    private final int MAX = 50;

    private int index = 1;// 不用 static 修饰,依旧存在线程安全问题

    @Override
    public void run() {
        while (index <= MAX) {
            System.out.println("当前柜台："+Thread.currentThread().getName()+"当前号码是：" + (index++));
            try {
//                Thread.sleep(100);
                TimeUnit.SECONDS.sleep(1);// jdk 1.5 后推荐使用的线程休眠方式，简单优雅
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        final TicketWindowRunnable ticketWindowRunnable = new TicketWindowRunnable();
        for (int i=0;i<4;i++){
            new Thread(ticketWindowRunnable,"ticketWindow_"+i).start();
        }
    }
}
